<link rel="stylesheet" href="/stylesheets/style.css">
<div class="row">
    <div class="container-header">
        <p class="container-title">包管理器</p>
    </div>
    <div class="container-content">

        <div class="container-content-top">

        </div>
        <div class="container-content-center row">
            <form>
                <div class="col-sm-12">
                    <div class="form-group">
                        <label class="control-label" for="title">显示名</label>
                        <input type="text" name="title" id="title" placeholder="请填写应用显示名" value="<%= guanjia ? guanjia.name : ""%>" class="form-control">
                    </div>
                    <div class="form-group">
                        <label class="control-label" for="desc">介绍</label>
                        <textarea name="desc" id="desc" placeholder="请填写应用介绍"  class="form-control"><%= guanjia ? guanjia.desc : ""%></textarea>
                    </div>
                    <div class="form-group">
                        <label class="control-label">关联应用</label>
                    </div>
                </div>

                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="control-label" for="ios">iOS</label>
                        <select id="ios" class="form-control">
                            <option value="">不关联</option>
                            <% if (ios) {
                                    for( app of ios ){

                                var selected = "";
                                console.log(guanjia.ios._id + ' ' + app._id)
                                if (guanjia && guanjia.ios && String(guanjia.ios._id) === String(app._id)) {
                                    selected = "selected";
                                }
                            %>
                            <%- `<option ${selected} value="${app._id}">${app.name}</option>` %>
                            <%
                            }
                            } %>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="control-label" for="ios">Android</label>
                        <select id="android" class="form-control">
                            <option value="">不关联</option>
                            <% if (android) {
                                    for( app of android ){

                                    var selected = "";
                                    if (guanjia && guanjia.android && String(guanjia.android._id) === String(app._id)) {
                                        selected = "selected";
                                    }
                            %>
                            <%- `<option ${selected} value="${app._id}">${app.name}</option>` %>
                            <%
                            }
                            } %>
                        </select>
                    </div>
                </div>
                <div class="col-sm-12" style="margin-top: 10px">
                    <button id="update-btn" type="button" class="btn btn-block btn-success">修改</button>
                </div>
            </form>
        </div>

        <div class="container-content-bottom">

        </div>

    </div>
</div>
<script language="JavaScript">
    $("#update-btn").click(function () {
        var name = $("#title").val();
        name = name || '';
        if (name.length == 0) {
            showError('显示名不能为空');
            return;
        }
        var desc = $('#desc').val();
        desc = desc || '';
        var ios = $('#ios option:selected').val();
        var android = $('#android option:selected').val();
        console.log(ios)
        $.post('/admin/guanjia/edit',{name:name,desc:desc,ios:ios,android:android},function (data) {
            if (data.code == 0) {
                showSuccess('已更新信息')
            } else  {
                showError(data.message);
            }
        })
    })
</script>